package com.usian.search.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usian.model.search.ApAssociateWords;
import com.usian.model.search.ApUserSearch;
import com.usian.model.user.pojos.ApUser;
import com.usian.search.mapper.AssociateMapper;
import com.usian.search.mapper.UserSearchMapper;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("search")
@Slf4j
public class SearchController {

    @Autowired
    private UserSearchMapper userSearchMapper;

    @Autowired
    private AssociateMapper associateMapper;

    @RequestMapping("user_SearchAdd")
    @Async("taskExecutor")
    public void user_SearchAdd(@RequestParam("entryId")Integer entryId, @RequestParam("keyword") String keyword){
        ApUser r = AppThreadLocalUtils.getUser();

        ApUserSearch apUserSearch1 = userSearchMapper.selectOne(Wrappers.<ApUserSearch>lambdaQuery()
                .eq(ApUserSearch::getEntryId,entryId)
                .eq(ApUserSearch::getKeyword,keyword));

        if(apUserSearch1!=null){
            log.info("不為非空不能添加");
            return;
        }
        ApUserSearch apUserSearch = new ApUserSearch();
        apUserSearch.setStatus(1);
        apUserSearch.setEntryId(entryId);
        apUserSearch.setCreatedTime(new Date());
        apUserSearch.setKeyword(keyword);
        userSearchMapper.insert(apUserSearch);
    }

    @RequestMapping("user_SearchEdit")
    public void user_SearchEdit(@RequestBody ApUserSearch apUserSearch){
        if(apUserSearch==null){
            log.info("參數爲空不能修改");
            return;
        }
        apUserSearch.setStatus(0);
        ApUser user = AppThreadLocalUtils.getUser();
        apUserSearch.setEntryId(user.getId());
        apUserSearch.setCreatedTime(new Date());
        userSearchMapper.updateById(apUserSearch);
    }

    @RequestMapping("user_SearchFind")
    public List<ApUserSearch> user_SearchFind(){
        ApUser user = AppThreadLocalUtils.getUser();
        LambdaQueryWrapper<ApUserSearch> q = new LambdaQueryWrapper<>();
        q.eq(ApUserSearch::getEntryId,user.getId());
        Page page = new Page(0,5);
        Page page1 = userSearchMapper.selectPage(page, q);
        return page1.getRecords();
    }

    @RequestMapping("findAllAssociate")
    public List<ApAssociateWords> findAllAssociate(){
        List<ApAssociateWords> apAssociateWords = associateMapper.selectList(null);
        return apAssociateWords;
    }
}
